package com.musicstore.web.controller;

import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.musicstore.biz.AlbumBiz;
import com.musicstore.biz.GenreBiz;
import com.musicstore.entity.AlbumEntity;
import com.musicstore.entity.GenreEntity;

@Controller  //springmvc文件扫描注释
public class AlbumController {
	@Autowired
	private GenreBiz genreBiz;
	@Autowired
	private AlbumBiz albumBiz;
	//最简单的方式
	@RequestMapping("/getAlbumTitle")//从控制器传数据到视图
	public String getAlbumTitle(Model model,String title,Integer pageNo)
	{
		System.out.println(pageNo+" AlbumController ");
		pageNo=(pageNo==null)?1:pageNo;
  		int pageSize =9;
		int startIndex = (pageNo-1)*pageSize;
		model.addAttribute("pageNo",pageNo);
		List<AlbumEntity> albumlist = albumBiz.getAlbumTitle(title,startIndex,pageSize);
		model.addAttribute("albumlist",albumlist);
		List<GenreEntity> genrelist = genreBiz.getAll();
		model.addAttribute("genrelist",genrelist);
		return "index";//返回视图名
	}
	//后台albumlist
	@RequestMapping("/albumlist")
	public String albumlist(Model model,Integer gid,String title,Integer page)
	{
		System.out.println("GID="+gid);
		gid=(gid==null)?0:gid;
		page=(page==null)?1:page;
		
		int pageSize=5;
		int startIndex=(page-1)*pageSize;
		List<AlbumEntity> albums=albumBiz.getAlbumPage(gid, title, startIndex, pageSize);
		int rows=albumBiz.getRowsCount(gid, title);
		//总页数
		int totalPages=(int)Math.ceil((double)rows/pageSize);
		//传递数据到视图
		model.addAttribute("albums",albums);
		model.addAttribute("totalPages",totalPages);
		model.addAttribute("genres",genreBiz.getAll());
		model.addAttribute("page",page);
		model.addAttribute("title",title);
		return "admin/album_list";
	}
	//后台albumadd
	@RequestMapping("/admin/albumAdd")
	public String albumAdd(Model model){
		model.addAttribute("genres",genreBiz.getAll());
		return "admin/album_add";
	}
	@RequestMapping("/albumSave")
	public String albumSave(AlbumEntity album,HttpServletRequest request,
			@RequestParam(name="coverImages",required=false)MultipartFile coverImages,HttpSession session)
	{
		if(!coverImages.isEmpty()){
		//获取物理路径
		String path=request.getServletContext().getRealPath("CoverImages");
		System.out.println(path);
		//文件名
		String filename=path+"/"+album.getId()+".jpg";
		System.out.println(filename);
		//将路径加到session里面
		session.setAttribute("filename", filename);
		//创建文件
		try {
			FileOutputStream  file=new FileOutputStream(filename);
			file.write(coverImages.getBytes());
			file.close();
			System.out.println("上传成功!");
		} catch (Exception e) {			
			e.printStackTrace();
		}
		}
		albumBiz.addAlbum(album);
		return "redirect:albumlist";
	}
	//后台albumadd
	@RequestMapping("/albumUpdate")
	public String albumUpdate(Model model, Integer id){
		AlbumEntity album = albumBiz.getById(id);
		model.addAttribute("album", album);
		model.addAttribute("genres",genreBiz.getAll());
		return "admin/admin_update";
	}
	@RequestMapping("/edit")
	public String edit(AlbumEntity album,HttpServletRequest request,
			@RequestParam(name="coverImages",required=false)MultipartFile coverImages)
	{
		if(!coverImages.isEmpty()){
		//获取物理路径
		String path=request.getServletContext().getRealPath("CoverImages");
		System.out.println(path);
		//文件名
		String filename=path+"/"+album.getId()+".jpg";
		System.out.println(filename);
		//创建文件
		try {
			FileOutputStream  file=new FileOutputStream(filename);
			file.write(coverImages.getBytes());
			file.close();
			System.out.println("上传成功!");
		} catch (Exception e) {			
			e.printStackTrace();
		}
		}
		albumBiz.updateAlbmu(album);
		return "redirect:albumlist";
	}
	@RequestMapping("/albumById")
	public String albumById(Model model, Integer id){
		AlbumEntity album = albumBiz.getById(id);
		model.addAttribute("album", album);
		model.addAttribute("genres",genreBiz.getAll());
		return "album_particular";
	}
}
